<?php

namespace app\api\controller;

use app\common\controller\Api;
/**
 * 预约登记接口
 *  预约，进入，离开
 */
class Reserve extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 预约登记
     *
     * @param string $username  账号
     * @param string $sid 学号
     */
    public function index()
    {
        $username = $this->request->post('username');
        $sid = $this->request->post('sid');
        // $username = '田振华';
        // $sid = '20161774';
        if (!$username || !$sid) {
            $this->error(__('Invalid parameters'));
        }

        //获取预约表的信息
        $where['id'] = 1;
        $row = \app\common\model\ReserveSetting::where($where)->find();
        //剩余预约人数为0
        if(empty($row['reserved_number']))
        {
            $this->error(__('The number of bookings in the library is full'));
        }

        $ret = $this->auth->enter($username, $sid);
        if ($ret) 
        {
            //成功预约后，更新剩余的预约数量
            if(empty($row['reserved_number'])){
                $params['reserved_number'] = $row['reserve_number']-1;
            }else{
                $params['reserved_number'] = $row['reserved_number']-1;
            }
            $params['add_time'] = time();

            //更新预约表的数量
            \app\common\model\ReserveSetting::where($where)->update($params);

            //预约成功，返回值
            $data = ['username' => $username,'sid'=>$sid];
            $this->success(__('Reserved in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }


    /**
     * 进入图书馆
     *
     * @param string $username  账号
     * @param string $sid 学号
     */    
    public function into()
    {
        $username = $this->request->post('username');
        $sid = $this->request->post('sid');
        // $username = '田振华';
        // $sid = '20161774';
        $where['nickname'] = $username;
        $where['sid'] = $sid;
        $row = \app\common\model\ReserveRecord::where($where)->find();
        if($row['states']==1){
            //处理进入状态
            $params['into_time']= time();
            $params['into_states']=1;
            $row->save($params);
            $this->success(__('Intoed in successful'));
        }else if($row['states']==0){
            //还未预约
            $this->error(__('Please make an appointment before entering'));
        }else{
            //已过期，重新预约
            $this->error(__('Appointment expired'));
        }

    }

    /**
     * 离开图书馆
     *
     * @param string $username  账号
     * @param string $sid 学号
     * 释放预约名额 
     */     
    public function leave(){
        $username = $this->request->post('username');
        $sid = $this->request->post('sid');
        // $username = '田振华';
        // $sid = '20161774';  
        $where['nickname'] = $username;
        $where['sid'] = $sid;
        $row = \app\common\model\ReserveRecord::where($where)->find();
        if($row['into_states']==1){
            //处理离开状态，做记录。
            $params['leave_time']= time();
            $params['leave_states']=1;
            $row->save($params);

            //给reservesetting表 释放记录，加回去一条记录
            //释放预约数量
            $id = 1;
            $sult = \app\common\model\ReserveSetting::find($id);
            if($sult){
                $finy = ($sult['reserved_number']+1);
                $sult -> reserved_number = $finy;
                $sult -> save();
            }
            $this->success(__('Leaved in successful'));
        }else{
            //找不到这条记录，提示错误
            $this->error(__('Record not exist'));            
        }   

    }
    
}
